package com.example.itas;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.classes.IP;

public class PaymentActivity extends Activity {

	ProgressDialog dialog;
	EditText payments, cardNumber, cardPassword;
	Button acceptButton;
	private MyApplication myApp;
	
	 private static final String TITLE = "TITLE";
	 private static final String DATE = "DATE";
	 private static final String TAG = "PaymentActivity";
	 
	 private final String PATH = "/IServer-war/webresources/entities.userlist/cash";
	 IP ip = new IP();
	 private final String HOST = ip.ip;
	 private final int PORT = 8080;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_payment);
		payments = (EditText)findViewById(R.id.edit_pay_edit);
		cardNumber = (EditText)findViewById(R.id.edit_pay_cardnum);
		cardPassword = (EditText)findViewById(R.id.edit_pay_cardpw);
		acceptButton = (Button)findViewById(R.id.pay_button);
		
		myApp = (MyApplication) getApplication();
		
		acceptButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				AsyncPayment al = new AsyncPayment();
				al.execute();
			}
			
		});
	}
	
	private class AsyncPayment extends AsyncTask<String, Void, String> {

		@Override
		protected void onPreExecute() {

			dialog = ProgressDialog.show(PaymentActivity.this, "",
					"Now Progressing...", true);
		}

		@Override
		protected String doInBackground(String... params) {

			String result = "false";

			HttpHost target = new HttpHost(HOST, PORT);
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(PATH);

			// set the MIME type for POST message body.
			//post.setHeader("Content-Type", "application/x-www-form-urlencoded");

			HttpEntity entity = null;

			List<NameValuePair> idpair = new ArrayList<NameValuePair>();
			idpair.add(new BasicNameValuePair("ID", myApp.getUid()));
			idpair.add(new BasicNameValuePair("CASH", "500"));

			try {
				post.setEntity(new UrlEncodedFormEntity(idpair));

				HttpResponse response = client.execute(target, post);

				// Status code 204 is success with "No content".
				// This happens when a RESTful method returns void.
				if (response.getStatusLine().getStatusCode() < 400) {
					result = "get";
				}

				entity = response.getEntity();
				
				String str = EntityUtils.toString(entity);

				// convert JSON to Person
				result = str;

				Log.d(TAG, "" + response.getStatusLine().getStatusCode());
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				Log.i("errorr:", "UnsupportedEncodingException");
			} catch (ClientProtocolException e) {
				e.printStackTrace();
				Log.i("errorr:", "ClientProtocolException");
			} catch (IOException e) {
				e.printStackTrace();
				Log.i("errorr:", "IOException");
			}
			return result;
		}

		@Override
		protected void onPostExecute(String result) {
			dialog.dismiss();

			Toast.makeText(PaymentActivity.this, "Payment Success", Toast.LENGTH_LONG).show();

		}
	}
}
